﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoBehaviour
{
    public GameObject panelContainer;

    public GameObject newGamePanelPrefab;
    public GameObject deckDisplayerPanelPrefab;
    public GameObject cardDetailDisplayerPanelPrefab;
    
    public GameObject rewardListPanelPrefab;
    public GameObject rewardCardPanelPrefab;


    public BaseEntityData playerEntityData;

    public Stack<GameObject> m_panelStack = new Stack<GameObject>();

    private void Awake()
    {
        if (GameObject.FindObjectsOfType<UIManager>().Length > 1)
        {
            Destroy(this.gameObject);
        }
        else
        {
            DontDestroyOnLoad(this);   
        }
    }

    public void PushPanel(GameObject panel)
    {
        SetTopPanelActiveState(false);

        panel.GetComponent<UIPanel>().isActive = true;
        panel.GetComponent<UIPanel>().UiManager = this;
        
        m_panelStack.Push(panel);
    }

    private void SetTopPanelActiveState(bool isActive)
    {
        if (m_panelStack.Count > 0)
        {
            var peek = m_panelStack.Peek();
            if (peek)
            {
                peek.GetComponent<UIPanel>().isActive = isActive;
            }
        }
    }
    
    public GameObject GetPanelContainer()
    {
        if (panelContainer == null)
        {
            panelContainer = GameObject.FindGameObjectWithTag(StaticDataManager.PANEL_CONTAINER_TAG);
        }

        return panelContainer;
    }
    
    public void DisplayNewGamePanel()
    {
        GameObject newGamePanel = Instantiate(newGamePanelPrefab, GetPanelContainer().transform, false);


        PushPanel(newGamePanel);
    }
    
    public void DisplayDeckDisplayerPanel()
    {
        GameObject deckDisplayerPanel = Instantiate(deckDisplayerPanelPrefab, GetPanelContainer().transform, false);
        
        // 然后调用里面的Displayer类的指定方法来显示卡牌数据
        FlushDeckPanel(deckDisplayerPanel);
        
        PushPanel(deckDisplayerPanel);
    }

    public void DisplayRewardListPanel(bool isNeedBackMapScene)
    {
        GameObject rewardListPanel = Instantiate(rewardListPanelPrefab, GetPanelContainer().transform, false);

        rewardListPanel.GetComponent<RewardManager>().isNeedBackMapScene = isNeedBackMapScene;
        
        PushPanel(rewardListPanel);
    }

    public void DisplayRewardCardPanel(int maxNum, CardPair[] cardPairs, GameObject belongItem = null)
    {
        GameObject rewardCardPanel = Instantiate(rewardCardPanelPrefab, GetPanelContainer().transform, false);
        
        rewardCardPanel.GetComponent<RewardCardManager>().Initial(maxNum, cardPairs, belongItem);

        PushPanel(rewardCardPanel);
    }

    public void CloseCurrentPanel()
    {
        // 从窗口栈中关闭最后进来的窗口
        GameObject panel = m_panelStack.Pop();
        panel.GetComponent<UIPanel>().isActive = false;
        
        Destroy(panel);
        
        SetTopPanelActiveState(true);
    }

    public void DisplayCardDetailPanel(CardPair pairData, Dictionary<int, CardPair> quickSlot)
    {
        GameObject cardDetailPanel = Instantiate(cardDetailDisplayerPanelPrefab, GetPanelContainer().transform, false);
        
        // 初始化卡牌信息
        CardDetailDisplayer cardDetailDisplayer = cardDetailPanel.GetComponent<CardDetailDisplayer>();
        
        cardDetailDisplayer.UiManager = this;

        cardDetailDisplayer.InitialCardData(pairData, ref quickSlot);
        PushPanel(cardDetailPanel);
    }

    public void SwitchToDeckPanel(bool isMap)
    {
        GameObject deckDisplayPanel = GameObject.FindWithTag(StaticDataManager.DECK_DISPLAY_PANEL_TAG);
        
        GameObject mapDisplayPanel = GameObject.FindWithTag(StaticDataManager.MAP_DISPLAY_PANEL_TAG);
        
        Vector3 deckPosition = Vector3.zero;
        Vector3 mapPosition = Vector3.zero;
        
        if (isMap)
        {
            deckPosition.x += deckDisplayPanel.GetComponent<RectTransform>().rect.width;
        }
        else
        {
            mapPosition.x -= mapDisplayPanel.GetComponent<RectTransform>().rect.width;
        }
        
        mapPosition.y = 0;
        mapPosition.z = 0;
        deckPosition.y = 0;
        deckPosition.z = 0;

        
        mapDisplayPanel.GetComponent<UIPanel>().MovePanelWithAnimation(mapPosition, .3f);
        deckDisplayPanel.GetComponent<UIPanel>().MovePanelWithAnimation(deckPosition, .3f);
    }

    public void FlushDeckPanel(GameObject deckDisplayerPanel)
    {
        if (deckDisplayerPanel == null)
        {
            deckDisplayerPanel = GameObject.FindWithTag(StaticDataManager.DECK_DISPLAY_PANEL_TAG);
        }

        if (deckDisplayerPanel == null)
        {
            Debug.Log("找不到卡牌面板");
            return;
        }
        
        DeckDisplayer deckDisplayer = deckDisplayerPanel.GetComponent<DeckDisplayer>();

        if (deckDisplayer == null)
        {
            Debug.Log("面板脚本丢失");
            return;
        }
        
        deckDisplayer.isActive = true;
        deckDisplayer.UiManager = this;
        deckDisplayer.InitialDeckData(playerEntityData);
    }
}
